package AppleCakeMining;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;


public class GlobalItemList {
	private HashMap<Integer, Item> items;
	private static GlobalItemList instance;
	
	private GlobalItemList() {
		items = new HashMap<Integer, Item>();
	}
	
	public static GlobalItemList getInstance() {
		if(instance == null) {
			instance = new GlobalItemList();
		}
		return instance;
	}

	public void addItem(Integer id, Transaction t) {
		if(!items.containsKey(id)) {
			Item newItem = new Item(id);
			items.put(id, newItem);
		}
		items.get(id).addTransaction(t);
	}
	
	public Item getItem(Integer id) {
		return items.get(id);
	}
	
	public int getTotalItems() {
		return items.size();
	}

	public ItemSetList calculateOneItemSets(int minSupport, int totalTransactions) {
		ItemSetList isl = new ItemSetList(1);
		ArrayList<Item> singleItems = new ArrayList<Item>(items.values());
		Collections.sort(singleItems);
		for(Item i:singleItems) {
			if(i.getSupport() >= minSupport) {
				ItemSet is = new ItemSet(i, totalTransactions);
				isl.addItemSet(is);
			}
		}
		items.clear();
		return isl;
	}

	public void clear() {
		items = new HashMap<Integer, Item>();
	}
}
